<?php
namespace app\common\logic;
class TokenLogic{

    private static $tokenJoin  = '&&&';

    private static $adminToken = 'admin';
    private static $adminKey  = 'wlsyelanxuieoqposlaksjka20230604';

    private static $accountToken = 'account';
    private static $accountKey  = 'wlsyelanxuieoqposlaksjka220608';

    private static $shopToken = 'shop';
    private static $shopKey  = 'wlsyelanxuieoqposlaksjka20230604';

    private static $miniToken = 'miniapp';
    private static $miniKey  = 'wlsyelanxuieoqposlaksjka';

    private static $memberQrToken = 'qrcode';
    private static $memberQrKey = 'klsiepwpxayejxhax001292838';

    private static $cashierToken = 'cashier';
    private static $cashierKey  = 'sjajyekxlslanxhe122!sio3';


    public static function getAppQrSignToken(){
        return '222woxius02sjk2lax';
    }

    public static function getMemberQrToken($id){
        $token = [
            self::$memberQrToken,
            $id,
            request()->time(),
        ];
        $tokenStr = join(self::$tokenJoin, $token);
        return auth_code($tokenStr,'ENCODE', self::$memberQrKey);
    }

    public static function getMemberQrDatas($token){
        $tokenStr = auth_code($token,'DECODE', self::$memberQrKey);
        if(empty($tokenStr)) return false;
        list($key,$id,$time) = explode(self::$tokenJoin,$tokenStr);
        if(empty($id)) return false;
        if(empty($key)) return false;
        if($key != self::$memberQrToken) return false;
        return $id;
    }

    public static function getAdminToken($adminId,$code)
    {
        $token = [
            self::$adminToken,
            $adminId,
            $code,
            request()->time(),
            request()->ip(),
        ];
        $tokenStr = join(self::$tokenJoin, $token);
        return auth_code($tokenStr,'ENCODE', self::$adminKey);
    }

    public static  function getAdminData($token){
        $tokenStr = auth_code($token,'DECODE', self::$adminKey);
        if(empty($tokenStr)) return false;
        list($key,$adminId,$code,$time,$ip) = explode(self::$tokenJoin,$tokenStr);
        if(empty($adminId)) return false;
        if(empty($code)) return false;
        if(empty($key)) return false;
        if($key != self::$adminToken) return false;
        return [
            'adminId'=>(int)$adminId,
            'code'=>$code
        ];
    }

    public static function getAccountToken($accountId,$code)
    {
        $token = [
            self::$accountToken,
            $accountId,
            $code,
            request()->time(),
            request()->ip(),
        ];
        $tokenStr = join(self::$tokenJoin, $token);
        return auth_code($tokenStr,'ENCODE', self::$accountKey);
    }
    
    public static  function getAccountData($token){
        $tokenStr = auth_code($token,'DECODE', self::$accountKey);
        if(empty($tokenStr)) return false;
        list($key,$accountId,$code,$time,$ip) = explode(self::$tokenJoin,$tokenStr);
        if(empty($accountId)) return false;
        if(empty($code)) return false;
        if(empty($key)) return false;
        if($key != self::$accountToken) return false;
        return [
            'accountId'=>(int)$accountId,
            'code'=>$code
        ];
    }

    public static function getShopToken($shopId,$accountId,$roleId=0)
    {
        $token = [
            self::$shopToken,
            $shopId,
            $accountId,
            $roleId,
            request()->time(),
            request()->ip(),
        ];
        $tokenStr = join(self::$tokenJoin, $token);
        return auth_code($tokenStr,'ENCODE', self::$shopKey);
    }

    public static  function getShopData($token){
        $tokenStr = auth_code($token,'DECODE', self::$shopKey);
        if(empty($tokenStr)) return false;
        list($key,$shopId,$accountId,$roleId,$time,$ip) = explode(self::$tokenJoin,$tokenStr);
        if(empty($shopId)) return false;
        if(empty($accountId)) return false;
        if(empty($key)) return false;
        if($key != self::$shopToken) return false;
        return [
            'accountId'=>(int)$accountId,
            'shopId'=>(int)$shopId,
            'roleId'=>(int)$roleId
        ];
    }


    public static function getMiniToken($memberId){
        $token = [
            self::$miniToken,
            $memberId,
            request()->time(),
            request()->ip(),
        ];
        $tokenStr = join(self::$tokenJoin, $token);
        return auth_code($tokenStr,'ENCODE', self::$miniKey);
    }

    public static function getMiniMemberId($token){
        $tokenStr = auth_code($token,'DECODE', self::$miniKey);
        if(empty($tokenStr)) return false;
        list($key,$memberId,$time,$ip) = explode(self::$tokenJoin,$tokenStr);
        if(empty($memberId)) return false;
        if(empty($key)) return false;
        if($key != self::$miniToken) return false;
        return $memberId;
    }

    public static function getStaffToken($staffId){
        $token = [
            self::$cashierToken,
            $staffId,
            request()->time(),
            request()->ip(),
        ];
        $tokenStr = join(self::$tokenJoin, $token);
        return auth_code($tokenStr,'ENCODE', self::$cashierKey);
    }

    public static function getStaffInfo($token){
        $tokenStr = auth_code($token,'DECODE', self::$cashierKey);

        if(empty($tokenStr)) return false;
        list($key,$staffId,$time,$ip) = explode(self::$tokenJoin,$tokenStr);
        if(empty($staffId)) return false;
        if(empty($key)) return false;
        if($key != self::$cashierToken) return false;
        return  (int)$staffId;
    }

    
}
